package com.sikao.app.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.EditText;

import com.sikao.app.R;
import com.sikao.app.bean.CategoryAttrBean;
import com.sikao.app.bean.CategoryAttrSubmitBean;
import com.sikao.app.ui.AddGoodsStepTwoActivity;
import com.sikao.app.utils.Utils;

public class MllAddStepPricesTypeOneItemAdapter extends BaseAdapter {

    private List<CategoryAttrBean.ResBean.AttrBean> objects = new ArrayList<CategoryAttrBean.ResBean.AttrBean>();

    private Context context;
    private LayoutInflater layoutInflater;

    public MllAddStepPricesTypeOneItemAdapter(Context context,List<CategoryAttrBean.ResBean.AttrBean> resTypeOne) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.objects=resTypeOne;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public CategoryAttrBean.ResBean.AttrBean getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.mll_add_step_prices_type_one_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews(getItem(position), (ViewHolder) convertView.getTag(),position);
        return convertView;
    }

    private void initializeViews(CategoryAttrBean.ResBean.AttrBean object, ViewHolder holder,final int position) {

        holder.tvName.setText(object.getName());

        //判断是否有默认值
       if (null==object.getDefault_value()||object.getDefault_value().size()<=0){//没有默认值
           holder.etValue.setText(object.getAttr_values().get(0).getValue());
       }else{
           holder.etValue.setText(object.getDefault_value().get(0).getValue());
           ((AddGoodsStepTwoActivity)context).getResTypeOne().get(position).getAttr_values().get(0).setValue(object.getDefault_value().get(0).getValue());
       }


        if (AddGoodsStepTwoActivity.canEdit){
            holder.etValue.setEnabled(true);
        }else{
            holder.etValue.setEnabled(false);
        }

        holder.etValue.setOnTouchListener(new View.OnTouchListener() {

            EditText et = null;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                et = (EditText) v;

                et.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        // String  changePrice = et.getText().toString();
                        ((AddGoodsStepTwoActivity)context).getResTypeOne().get(position).getAttr_values().get(0).setValue(et.getText().toString());
                       // ((AddGoodsStepTwoActivity)context).getResTypeOne().get(position).getAttr_values().get(0).setChecked(true);
                      /*  for (int i=0;i<((AddGoodsStepTwoActivity)context).getResTypeOne().size();i++){
                            Utils.showLog("===商品编辑中文本：i="+i+" name="+((AddGoodsStepTwoActivity)context).getResTypeOne().get(i).getName()+" value="+((AddGoodsStepTwoActivity)context).getResTypeOne().get(i).getAttr_values().get(0).getValue());
                        }*/
                    }
                });
                return false;
            }
        });

    }

    protected class ViewHolder {
        private TextView tvName;
    private EditText etValue;

        public ViewHolder(View view) {
            tvName = (TextView) view.findViewById(R.id.tv_name);
            etValue = (EditText) view.findViewById(R.id.et_value);
        }
    }
}
